package cn.itcast.jobs;

import cn.itcast.constant.RedisConstant;
import cn.itcast.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

@Component
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    @Scheduled(cron="0 0 23 * * ?")
    public void clearImg() {
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff != null) {
            for (String s : sdiff) {
                //七牛云删除
                QiniuUtils.deleteFileFromQiniu(s);
                //redis删除
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,s);
            }
        }
    }

    @Scheduled(cron="0 0 1 ? * L")
    public void clearDBImg() {
        Jedis jedis = jedisPool.getResource();
        jedis.del(RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        jedis.del(RedisConstant.SETMEAL_PIC_RESOURCES);
    }
}
